package cn.com.ddapp.ddm.ui.mine;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.bean.mine.BankList;
import cn.com.ddapp.ddm.bean.mine.Card;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.StringUtils;
import cn.com.ddapp.ddm.widget.SpinnerPopView;

public class BindBankActivity extends BaseActivity {

    //选择银行
    @BindView(R.id.bd_select_bankcrl)
    RelativeLayout bd_select_bankcrl;
    //银行
    @BindView(R.id.getbank)
    TextView getbank;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.name)
    EditText cardName;
    @BindView(R.id.bankBranch)
    EditText bankBranch;
    @BindView(R.id.bankAccount)
    EditText bankAccount;
    @BindView(R.id.bd_bank)
    Button bdBank;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;

    SpinnerPopView.MSpinnerAdapter spinnerAdapter;
    List<BankList.ListBean> stringList;
    SpinnerPopView<BankList.ListBean> spinnerPopView;
    String userId,name,branch,account,bankName;
    int bankId;



    @Override
    public void initView() {
        setTitle("绑定银行卡");
        stringList = new ArrayList<BankList.ListBean>();
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    BankList card = gson.fromJson(response, BankList.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(card.getResult()))) {
                        BaseApplication.getIntstance().showToast(card.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(card.getResult()))) {
                        stringList.addAll(card.getList());
                    }
                }
            };
            //银行卡列表
            OKUtilAPI.post(OKUtilAPI.bankList, "", callback);
        }
        spinnerPopView = new SpinnerPopView<BankList.ListBean>(this, stringList, itemClickListener);
        spinnerPopView.setOnDismissListener(dismissListener);
        bd_select_bankcrl.setOnClickListener(this);
    }

    @Override
    public void initData() {}

    @Override
    public int getContentViewId() {
        return R.layout.activity_bind_bank;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.bd_select_bankcrl,R.id.bd_bank,R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bd_select_bankcrl:
                spinnerPopView.setWidth(getbank.getWidth());
                spinnerPopView.showAsDropDown(getbank);
//                setTextImage(R.mipmap.upnow);
                break;
            case R.id.bd_bank:
                userId = SPUtils.getStringData(this, SPUtils.USERID);
                //name,branch,account,bankName
                name = cardName.getText().toString();
                branch = bankBranch.getText().toString();
                account = bankAccount.getText().toString();
                bankName = getbank.getText().toString();
                if ("".equals(account)){
                    BaseApplication.getIntstance().showToast("请输入银行卡号", Toast.LENGTH_SHORT, 0, Gravity.TOP);
                }
                else if (!NetUtils.isConnected(this)) {
                    Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
                } else {
                    mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                    OkUtilCallback callback = new OkUtilCallback(mEmptyLayout) {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            Entity card = gson.fromJson(response, Entity.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(card.getResult())) && card.getLogin()) {
                                BaseApplication.getIntstance().showToast(card.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(card.getResult()))) {
                                BaseApplication.getIntstance().showToast(card.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(card.getResult()))) {
                                BaseApplication.getIntstance().showToast(card.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                finish();
                            }
                        }
                    };
                    //绑定银行卡
                    /*userId int[Y] 会员ID
                    name string[Y] 支付宝姓名
                    account string[Y] 支付宝账号
                    bankName string[Y] 银行名称
                    branch string[Y] 支行名称
                    bankId int银行卡列表对应的id*/
                    OKUtilAPI.post(OKUtilAPI.addBank, "userId=" + userId + "&name="+name +"&account="+account+"&bankName="+bankName+"&branch="+branch+"&bankId="+bankId, callback);
                    Log.e("1111", "userId=" + userId + "&name="+name +"&account="+account+"&bankName="+bankName+"&branch="+branch+"&bankId="+bankId);
                }
                break;
            case R.id.back :
                finish();
                break;
        }
    }

    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            spinnerPopView.dismiss();
            getbank.setText(stringList.get(i).getName());
            bankId = stringList.get(i).getId();
        }
    };

    private PopupWindow.OnDismissListener dismissListener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
//            setTextImage(R.mipmap.synthesize);
        }
    };

}
